package lib

func init() {
	Probs = append(Probs, Problem{
		Num:         1419,
		Discription: "组成叫声序列的最少青蛙数",
		Level:       2,
		Labels: map[string]int{
			"循环数组": 1,
		},
	})
}

//维护一个当前处于不同叫声状态的青蛙数
//叫完最后一声的青蛙可以复用去叫第一声
//没有可复用的青蛙去叫第一声就创建一个青蛙
func MinNumberOfFrogs(croakOfFrogs string) int {
	getIndex := func(ch byte) int {
		switch ch {
		case 'c':
			return 0
		case 'r':
			return 1
		case 'o':
			return 2
		case 'a':
			return 3
		case 'k':
			return 4
		}

		return 0
	}

	var frogState [5]int
	frogCount := 0
	for i := range croakOfFrogs {
		ch := croakOfFrogs[i]
		index := getIndex(ch)
		if index == 0 {
			if frogState[4] > 0 {
				frogState[4]--
			} else {
				frogCount++
			}
			frogState[0]++
			continue
		}

		if frogState[index-1] == 0 {
			return -1
		}

		frogState[index-1]--
		frogState[index]++
	}

	for i := 0; i <= 3; i++ {
		if frogState[i] > 0 {
			return -1
		}
	}

	return frogState[4]
}
